<?php
/*
 * Laconica - a distributed open-source microblogging tool
 * Copyright (C) 2008, Controlez-Vous, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

if (!defined('LACONICA')) { exit(1); }

require_once(INSTALLDIR.'/classes/Channel.php');


class Command
{
    
    var $user = null;
    
    function __construct($user=null)
    {
        $this->user = $user;
    }
    
    function execute($channel)
    {
        return false;
    }
    
    function formatVietnamese($str)
    {
    $str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", 'a', $str);
    $str = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", 'e', $str);
    $str = preg_replace("/(ì|í|ị|ỉ|ĩ)/", 'i', $str);
    $str = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", 'o', $str);
    $str = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", 'u', $str);
    $str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", 'y', $str);
    $str = preg_replace("/(đ)/", 'd', $str);
    $str = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/", 'A', $str);
    $str = preg_replace("/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", 'E', $str);
    $str = preg_replace("/(Ì|Í|Ị|Ỉ|Ĩ)/", 'I', $str);
    $str = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/", 'O', $str);
    $str = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", 'U', $str);
    $str = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", 'Y', $str);
    $str = preg_replace("/(Đ)/", 'D', $str);

    return $str; 
    }
}

class UnimplementedCommand extends Command
{
    function execute($channel)
    {
        $channel->error($this->user, _("Xin loi ban, lenh nay chua duoc phat trien."));
    }
}

class TrackingCommand extends UnimplementedCommand
{
}

class TrackOffCommand extends UnimplementedCommand
{
}

class TrackCommand extends UnimplementedCommand
{
    var $word = null;
    function __construct($user, $word)
    {
        parent::__construct($user);
        $this->word = $word;
    }
}

class UntrackCommand extends UnimplementedCommand
{
    var $word = null;
    function __construct($user, $word)
    {
        parent::__construct($user);
        $this->word = $word;
    }
}

class NudgeCommand extends UnimplementedCommand
{
    var $other = null;
    function __construct($user, $other)
    {
        parent::__construct($user);
        $this->other = $other;
    }
}

class InviteCommand extends UnimplementedCommand
{
    var $other = null;
    function __construct($user, $other)
    {
        parent::__construct($user);
        $this->other = $other;
    }
}

class StatsCommand extends Command
{
    function execute($channel)
    {

        $subs = new Subscription();
        $subs->subscriber = $this->user->id;
        $subs_count = (int) $subs->count() - 1;

        $subbed = new Subscription();
        $subbed->subscribed = $this->user->id;
        $subbed_count = (int) $subbed->count() - 1;

        $notices = new Notice();
        $notices->profile_id = $this->user->id;
        $notice_count = (int) $notices->count();
        
        $channel->output($this->user, sprintf(_("Quan tam: %1\$s <br />".
                                   "Theo duoi: %2\$s <br />".
                                   "So tin nhan: %3\$s"),
                                 $subs_count,
                                 $subbed_count,
                                 $notice_count));
    }
}

class FavCommand extends Command
{
    
    var $other = null;
    
    function __construct($user, $other)
    {
        parent::__construct($user);
        $this->other = $other;
    }
    
    function execute($channel)
    {
        
        $recipient = 
          common_relative_profile($this->user, common_canonical_nickname($this->other));
        
        if (!$recipient) {
            $channel->error($this->user, _('Khong co thanh vien nay.'));
            return;
        }
        $notice = $recipient->getCurrentNotice();
        if (!$notice) {
            $channel->error($this->user, _('Thanh vien nay chua co tin nhan.'));
            return;
        }
        
        $fave = Fave::addNew($this->user, $notice);

        if (!$fave) {
            $channel->error($this->user, _('Khong the them vao ua thich.'));
            return;
        }

        $other = User::staticGet('id', $recipient->id);
        
        if ($other && $other->id != $user->id) {
            if ($other->email && $other->emailnotifyfav) {
                mail_notify_fave($other, $this->user, $notice);
            }
        }
        
        $this->user->blowFavesCache();
        
        $channel->output($this->user, _('Da them tin nhan vao ua thich.'));
    }
}

class WhoisCommand extends Command
{
    var $other = null;
    function __construct($user, $other)
    {
        parent::__construct($user);
        $this->other = $other;
    }
    
    function execute($channel)
    {
        $recipient = 
          common_relative_profile($this->user, common_canonical_nickname($this->other));
        
        if (!$recipient) {
            $channel->error($this->user, _('Khong co thanh vien nay.'));
            return;
        }
        
        $whois = sprintf(_("%1\$s (%2\$s)"), $recipient->nickname,
                         $recipient->profileurl);
        if ($recipient->fullname) {
            $whois .= "<br />" . sprintf(_('Ten: %s'), $recipient->fullname);
        }
        if ($recipient->location) {
            $whois .= "<br />" . sprintf(_('Dia chi: %s'), $recipient->location);
        }
        if ($recipient->homepage) {
            $whois .= "<br />" . sprintf(_('Trang chu: %s'), $recipient->homepage);
        }
        if ($recipient->bio) {
            $whois .= "<br />" . sprintf(_('Gioi thieu: %s'), $recipient->bio);
        }
        $channel->output($this->user, $this->formatVietnamese($whois));
    }
}

class MessageCommand extends Command
{
    var $other = null;
    var $text = null;
    function __construct($user, $other, $text)
    {
        parent::__construct($user);
        $this->other = $other;
        $this->text = $text;
    }
    
    function execute($channel)
    {
        $other = User::staticGet('nickname', common_canonical_nickname($this->other));
        $len = mb_strlen($this->text);
        if ($len == 0) {
            $channel->error($this->user, _('Khong co noi dung!'));
            return;
        } else if ($len > 140) {
            $content = common_shorten_links($content);
            if (mb_strlen($content) > 140) {
                $channel->error($this->user, sprintf(_('Tin nhan qua dai - toi da 140 ky tu, ban da danh %d ky tu'), $len));
                return;
            }
        }
        
        if (!$other) {
            $channel->error($this->user, _('Khong co thanh vien.'));
            return;
        } else if (!$this->user->mutuallySubscribed($other)) {
            $channel->error($this->user, _('Ban khong the goi tin nhan den thanh vien nay.'));
            return;
        } else if ($this->user->id == $other->id) {
            $channel->error($this->user, _('Dung goi tin nhan cho chinh ban chu ^_^.'));
            return;
        }
        $message = Message::saveNew($this->user->id, $other->id, $this->text, $channel->source());
        if ($message) {
            $channel->output($this->user, sprintf(_('Da goi tin nhan cho %s'), $this->other));
        } else {
            $channel->error($this->user, _('Loi khi goi tin nhan.'));
        }
    }
}

class GetCommand extends Command
{
    
    var $other = null;
    
    function __construct($user, $other)
    {
        parent::__construct($user);
        $this->other = $other;
    }
    
    function execute($channel)
    {
        $target_nickname = common_canonical_nickname($this->other);
        
        $target =
          common_relative_profile($this->user, $target_nickname);

        if (!$target) {
            $channel->error($this->user, _('Khong co thanh vien nay.'));
            return;
        }
        $notice = $target->getCurrentNotice();
        if (!$notice) {
            $channel->error($this->user, _('Thanh vien nay khong co tin nhan.'));
            return;
        }
        $notice_content = $this->formatVietnamese($notice->content);
        
        $channel->output($this->user, $target_nickname . ": " . $notice_content);
    }
}

class SubCommand extends Command
{
    
    var $other = null;
    
    function __construct($user, $other)
    {
        parent::__construct($user);
        $this->other = $other;
    }
    
    function execute($channel)
    {
        
        if (!$this->other) {
            $channel->error($this->user, _('Ban can nhap ten thanh vien ban muon theo duoi'));
            return;
        }
        
        $result = subs_subscribe_user($this->user, $this->other);
        
        if ($result == 'true') {
            $channel->output($this->user, sprintf(_('Da theo duoi %s'), $this->other));
        } else {
            $channel->error($this->user, $result);
        }
    }
}

class UnsubCommand extends Command
{

    var $other = null;
    
    function __construct($user, $other)
    {
        parent::__construct($user);
        $this->other = $other;
    }

    function execute($channel)
    {
        if(!$this->other) {
            $channel->error($this->user, _('Ban can nhap ten thanh vien ban muon bo theo duoi'));
            return;
        }
        
        $result=subs_unsubscribe_user($this->user, $this->other);
        
        if ($result) {
            $channel->output($this->user, sprintf(_('Bo theo duoi voi %s'), $this->other));
        } else {
            $channel->error($this->user, $result);
        }
    }
}

class OffCommand extends Command
{
    var $other = null;
    function __construct($user, $other=null)
    {
        parent::__construct($user);
        $this->other = $other;
    }
    function execute($channel)
    {
        if ($other) {
            $channel->error($this->user, _("Command not yet implemented."));
        } else {
            if ($channel->off($this->user)) {
                $channel->output($this->user, _('Tat thong bao.'));
            } else {
                $channel->error($this->user, _('Khong the tat thong bao.'));
            }
        }
    }
}

class OnCommand extends Command
{
    var $other = null;
    function __construct($user, $other=null)
    {
        parent::__construct($user);
        $this->other = $other;
    }
    
    function execute($channel)
    {
        if ($other) {
            $channel->error($this->user, _("Command not yet implemented."));
        } else {
            if ($channel->on($this->user)) {
                $channel->output($this->user, _('Bat thong bao.'));
            } else {
                $channel->error($this->user, _('Khong the bat thong bao.'));
            }
        }
    }
}

class HelpCommand extends Command
{
    function execute($channel)
    {
        $channel->output($this->user,
                         _("<br >Commands: <br >".
                           "on - turn on notifications <br />".
                           "off - turn off notifications <br />".
                           "guide - show this help <br />".
                           "follow [nickname] - subscribe to user <br />".
                           "leave [nickname] - unsubscribe from user <br />".
                           "d [nickname] [text] - direct message to user <br />".
                           "get [nickname] - get last notice from user <br />".
                           "whois [nickname] - get profile info on user <br />".
                           "fav [nickname] - add user's last notice as a 'fave' <br />".
                           "stats - get your stats <br />".
                           "stop - same as 'off' <br />".
                           "quit - same as 'off' <br />".
                           "sub [nickname] - same as 'follow' <br />".
                           "unsub [nickname] - same as 'leave' <br />".
                           "last [nickname] - same as 'get' <br />"));
    }
}

class HelpViCommand extends Command
{
    function execute($channel)
    {
        $channel->output($this->user,
                         _("<br >Cac lenh co ban: <br >".
                           "bat - bat chuc nang thong bao <br />".
                           "tat - tat chuc nang thong bao <br />".
                           "huongdan - hien thi bang huong dan <br />".
                           "theoduoi [nickname] - theo duoi thanh vien <br />".
                           "tubo [nickname] - bo theo duoi thanh vien <br />".
                           "d [nickname] [noidung] - goi tin nhan den thanh vien <br />".
                           "xem [nickname] - xem tin nhan moi nhat cua thanh vien <br />".
                           "aila [nickname] - xem thong tin cua thanh vien <br />".
                           "uathich [nickname] - them tin nhan moi nhat cua thanh vien vao ua thich <br />".
                           "tinhtrang - xem trang thai hien tai cua ban <br />".
                           "dung - tuong tu giong 'tat' <br />".
                           "thoat - tuong tu giong 'tat' <br />".
                           "theo [nickname] - tuong tu 'theoduoi' <br />".
                           "botheoduoi [nickname] - tuong tu 'tubo' <br />".
                           "moinhat [nickname] - tuong tu 'xem' <br />"));
    }
}